//
//  HSClassTableController.swift
//  Home_school_晓林
//
//  Created by 张晓林 on 16/9/14.
//  Copyright © 2016年 张晓林. All rights reserved.
//

import UIKit

class HSClassTableController: UIViewController {
    
    /// 周标签宽度和高度
    fileprivate let weekLabelH: CGFloat = 44
    /// 课段标签宽度
    fileprivate let weekLabelW: CGFloat = UIScreen.xl_ScreenW() / 8
    /// 集合视图
    fileprivate var collectionView: UICollectionView?
    /// 数据源数组
    lazy var dataArray: [Int] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // 背景色
        view.backgroundColor = UIColor.white
        automaticallyAdjustsScrollViewInsets = false
        // 设置视图
        setupUI()
        download()
    }
    
    func download() {
        
        NetworkTools.shared.requestWithGet(urlStr: "zao/interface/ZH-schoolname-S-login", parameters: ["code":"5101","password":"123456"]) { (data, error) in
            print(data,error)
        }
    }
    
    /// 周标题视图
    fileprivate func setupUI() {
        // 设置左边返回按钮
        navigationController?.navigationBar.barTintColor = #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)
        let backBtn = UIBarButtonItem(image: "返回按钮", title: "课表", target: self, action: #selector(backBtnClicked))
        navigationItem.leftBarButtonItem = backBtn
        
        // 添加集合视图
        setupCollectionView()
        
        // 添加顶部周标题视图
        let weekTitleArray = ["","一","二","三","四","五","六","日"]
        
        for i in 0..<8 {
            let rect = CGRect(x: CGFloat(i)*weekLabelW, y: 64, width: weekLabelW, height: weekLabelH)
            let label = UILabel(frame: rect, title: weekTitleArray[i], color: #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1), fontSize: 15)
            view.addSubview(label)
        }
        
        // 添加左边课时标题视图
        for i in 0..<9 {
            // 计算frame
            let y = weekLabelH+64+(CGFloat(i)*weekLabelH)
            
            if i == 4 {
                // 设置午休视图
                let rect = CGRect(x: 10, y: y+1, width: UIScreen.xl_ScreenW()-20, height: weekLabelH-1)
                let btn = UIButton(frame: rect, bgImage: "curriculum_wuxiu_bg", title: "午休时间")
                view.addSubview(btn)
            }else {
                // 设置左边标签
                let rect = CGRect(x: 0, y: y, width: weekLabelW, height: weekLabelH)
                let label = UILabel(frame: rect, title: "\(i+1)", color: #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1), fontSize: 15)
                view.addSubview(label)
            }
            // 分割线
            let line = UIView(frame: CGRect(x: 10, y: y+weekLabelH, width: UIScreen.xl_ScreenW()-20, height: 1))
            line.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
            view.addSubview(line)
        }
        
       
    }
    
    // 设置显示课程内容的集合视图
    fileprivate func setupCollectionView() {
        // 设置课表内容视图
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: weekLabelW-1, height: weekLabelH-1)
        flowLayout.minimumLineSpacing = 1
        flowLayout.minimumInteritemSpacing = 1
        flowLayout.scrollDirection = .horizontal
        // 初始化集合视图
        collectionView = UICollectionView(frame: CGRect(x: weekLabelW, y: weekLabelH + 64, width: weekLabelW*7, height: weekLabelH*9), collectionViewLayout: flowLayout)
        collectionView?.backgroundColor = UIColor.white
        collectionView?.dataSource = self
        collectionView?.delegate = self
        collectionView?.register(HSClassTableCell.self, forCellWithReuseIdentifier: "classTableCell")
        // 添加课程集合视图
        view.addSubview(collectionView!)
    }
    
    // 返回
    @objc fileprivate func backBtnClicked() {
        dismiss(animated: true, completion: nil)
    }
    
    
}

// MARK: - 数据源方法
extension HSClassTableController: UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 45
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "classTableCell", for: indexPath) as? HSClassTableCell
        
        
        
        return cell!
    }
    
}












